#include "transferwindow.h"

TransferWindow ::TransferWindow() {
    CreateMainWindow();
    isShowFileTable = true;
    isFileSelected = false;
}

TransferWindow::~TransferWindow () {
    delete fileTable;
    delete deviceTable;
    delete stack;
    delete switchButton;
}

void TransferWindow::CreateMainWindow() {
    setWindowTitle("Transfer File");
    setWindowIcon(QIcon(":/material/Ui/transfer.png"));

    setMinimumSize(720, 240);
    setMaximumSize(720, 240);

    SetWidgets();
}

void TransferWindow::SetWidgets() {
    fileTable = new QTableWidget(1, 5);
    QStringList fileHeaders;
    fileHeaders.append("File Path");
    fileHeaders.append("File Name");
    fileHeaders.append("File Type");
    fileHeaders.append("File Size");
    fileHeaders.append("Add File");
    fileTable -> setHorizontalHeaderLabels(fileHeaders);
    fileTable -> horizontalHeader() -> setSectionResizeMode(QHeaderView::Stretch);
    fileTable -> verticalHeader() -> hide();
    fileTable -> setSelectionMode(QAbstractItemView::NoSelection);

    QPushButton *pBtn = new QPushButton();
    pBtn -> setIcon(QIcon(":/material/Ui/add_file.png"));
    pBtn -> setIconSize(QSize(20, 20));
    pBtn -> setStyleSheet("background-color: #FFFFFF;");
    connect(pBtn, SIGNAL(clicked(bool)), this, SLOT(SlotAddFile()));
    fileTable -> setCellWidget(0, 4, pBtn);

    deviceTable = new QTableWidget(0, 3);
    QStringList deviceHeaders;
    deviceHeaders.append("Device Name");
    deviceHeaders.append("Device Ip");
    deviceHeaders.append("Device System");
    deviceTable -> setHorizontalHeaderLabels(deviceHeaders);
    deviceTable -> horizontalHeader() -> setSectionResizeMode(QHeaderView::Stretch);
    deviceTable -> verticalHeader() -> hide();

    stack = new QStackedWidget();
    stack -> addWidget(fileTable);
    stack -> addWidget(deviceTable);

    switchButton = new QPushButton();
    switchButton -> setIcon(QIcon(":/material/Ui/ok.png"));
    connect(switchButton, SIGNAL(clicked(bool)), this, SLOT(SlotSwitch()));
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout -> addStretch();
    buttonLayout -> addWidget(switchButton);

    QVBoxLayout *layout = new QVBoxLayout();
    layout -> addWidget(stack);
    layout -> addLayout(buttonLayout);

    this -> setLayout(layout);
}

void TransferWindow::SlotAddFile() {
    std::string mainDirectory = GetMainDirectory();
    QString file = QFileDialog::getOpenFileName(this, tr("Select File"), QString::fromStdString(mainDirectory), tr("All File(*.*)"));
    if (file != "") {
        QFileInfo fileInfo(file);

        QTableWidgetItem *filePath = new QTableWidgetItem();
        filePath -> setText(fileInfo.filePath());
        filePath -> setTextAlignment(Qt::AlignCenter);
        fileTable -> setItem(fileTable -> rowCount() - 1, 0, filePath);
        QTableWidgetItem *fileName = new QTableWidgetItem();
        fileName -> setText(fileInfo.baseName());
        fileName -> setTextAlignment(Qt::AlignCenter);
        fileTable -> setItem(fileTable -> rowCount() - 1, 1, fileName);
        QTableWidgetItem *fileType = new QTableWidgetItem();
        fileType -> setText(fileInfo.suffix());
        fileType -> setTextAlignment(Qt::AlignCenter);
        fileTable -> setItem(fileTable -> rowCount() - 1, 2, fileType);
        QTableWidgetItem *fileSize = new QTableWidgetItem();
        QString size;
        if (fileInfo.size() <= 1024) {
            size = QString::number(double(fileInfo.size()));
            size.append("B");
        }
        else if (fileInfo.size() > 1024 && fileInfo.size() <= 1024 * 1024) {
            size = QString::number(double(fileInfo.size())/1024);
            size.append("KB");
        }
        else if (fileInfo.size() > (1024 * 1024)) {
            size = QString::number(double(fileInfo.size())/1024/1024);
            size.append("MB");
        }
        fileSize -> setText(size);
        fileSize -> setTextAlignment(Qt::AlignCenter);
        fileTable -> setItem(fileTable -> rowCount() - 1, 3, fileSize);

        fileTable -> setRowCount(fileTable -> rowCount() + 1);
        QPushButton *pBtn = new QPushButton();
        pBtn -> setIcon(QIcon(":/material/Ui/add_file.png"));
        pBtn -> setIconSize(QSize(20, 20));
        pBtn -> setStyleSheet("background-color: #FFFFFF;");
        connect(pBtn, SIGNAL(clicked(bool)), this, SLOT(SlotAddFile()));
        fileTable -> setCellWidget(fileTable -> rowCount() - 1, 4, pBtn);
        // TODO
    }
}

void TransferWindow::SlotSwitch() {
    isShowFileTable = !isShowFileTable;
    if (!isShowFileTable) {
        stack -> setCurrentIndex(1);
        switchButton -> setIcon(QIcon(":/material/Ui/send.png"));

        // QList<QTableWidgetItem *> selectedItemList = fileTable -> selectedItems();
    } else {
        stack -> setCurrentIndex(0);
        switchButton -> setIcon(QIcon(":/material/Ui/ok.png"));

        fileTable -> clearContents();
        fileTable -> setRowCount(1);
        QPushButton *pBtn = new QPushButton();
        pBtn -> setIcon(QIcon(":/material/Ui/add_file.png"));
        pBtn -> setIconSize(QSize(20, 20));
        pBtn -> setStyleSheet("background-color: #FFFFFF;");
        connect(pBtn, SIGNAL(clicked(bool)), this, SLOT(SlotAddFile()));
        fileTable -> setCellWidget(0, 4, pBtn);
        // TODO

        deviceTable -> clearContents();
        deviceTable -> setRowCount(0);
        this -> close();
    }
}
